import { createRouter, createWebHistory } from "vue-router";
import { logout } from "../assets/axios/login";

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    //登录
    path: "/login",
    name: "login",
    component: () => import("../views/Login/LoginView.vue"),
  },
  //手机登录
  {
    path: "/phone",
    name: "phone",
    component: () => import("../views/Login/PhoneLogin.vue"),
  },
  {
    path: "/index",
    redirect: "/home",
  },
  {
    path: "/index",
    name: "index",
    component: () => import("../views/IndexView.vue"),
    meta: {
      keepAlive: true,
    },
    children: [
      {
        //发现
        path: "/home",
        name: "home",
        component: () => import("../views/Home/HomeView.vue"),
        meta: {
          index: true,
        },
      },
      {
        //播客
        path: "/podcast",
        name: "podcast",
        component: () => import("../views/Podcast/PodcastView.vue"),
        meta: {
          index: true,
        },
      },
      {
        //我的
        path: "/mine",
        name: "mine",
        component: () => import("../views/Mine/MineView.vue"),
        meta: {
          index: true,
        },
      },
      {
        //云村
        path: "/cloud",
        name: "cloud",
        component: () => import("../views/Cloud/CloudView.vue"),
        meta: {
          index: true,
        },
      },
      {
        //关注
        path: "/attention",
        name: "attention",
        component: () => import("../views/Attention/AttentionView.vue"),
        meta: {
          index: true,
        },
      },
      //推荐歌单详情页
      {
        path: "/playitem",
        name: "playitem",
        meta: {
          keepAlive: false,
        },
        component: () => import("../views/PlayItem/PlayItem.vue"),
      },
      //搜索页
      {
        path: "/search",
        name: "search",
        component: () => import("../views/Search/SearchView.vue"),
      },
      //推荐
      {
        path: "/recommend",
        name: "recommend",
        component: () => import("../views/recommend/recommend.vue"),
      },
      {
        path: "/:pathMatch(.*)",
        redirect: "/index",
        hidden: true,
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  // console.log(to, from);
  // if (localStorage.getItem("hm") === "xhx") {
  //   next();
  // } else {
  // loginStatus()
  //   .then((res) => {
  // if (res.data.data.account.status === -10) {
  //   if (to.path == "/login" || to.path == "/phone") {
  //     next();
  //   } else {
  //     next("/login");
  //   }
  // } else if (res.data.data.account.status === 0) {
  if (to.path == "/login" || to.path == "/phone") {
    logout();
  }
  next();
  // }
  // })
  // .catch((err) => {
  //   next("/login");
  // });
  // if (to.path == "/login" || to.path == "/phone") {
  //   next();
  // }
  // }
});

export default router;
